package com.lx.upload.web;

import com.lx.bean.Result;
import com.lx.upload.bean.UploadPropertise;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import java.io.File;
import java.io.IOException;
import java.util.ArrayList;
import java.util.List;
import java.util.UUID;

@RestController
@RequestMapping("upload")
public class UploadDo {

    @Autowired
    UploadPropertise uploadPropertise;

    @GetMapping("config")
    UploadPropertise uploadPropertise(){
        return uploadPropertise;
    }

    @PostMapping("upload")
    public Result upload(@RequestParam("file") MultipartFile file,
                         String type) throws IOException {
        UploadPropertise.FileConfig fileConfig = uploadPropertise.getConfig().get(type);
        // 获取文件名
        String filename = file.getOriginalFilename();
        String prefix = UUID.randomUUID().toString().replaceAll("-", "");
        String suffix = filename.replaceFirst("(.+)(\\.\\w+)", "$2");

        String[] suffixs = fileConfig.getSuffix().split(",");
        // 判断后缀是否合法
        boolean isLegalSuffix = false;
        for (String s : suffixs) {
            System.out.println("suffix.replace(\".\", \"\") = " + suffix.replace(".", ""));
            System.out.println("s = " + s);
            if (s.equals(suffix.replace(".", ""))) {
                isLegalSuffix = true;
                break;
            }
        }
        if (!isLegalSuffix) {
            return new Result(1, "文件不合法, 请重新上传", null);
        }
        filename = prefix + suffix;

        //文件夹不存在就创建
        String diskpath = fileConfig.getDiskpath();
        File disPath = new File(diskpath);
        if(!diskpath.endsWith("/")){
            diskpath = diskpath +  "/";
        }
        if (!disPath.exists()) {
            disPath.mkdirs();
        }
        File fileObj = new File(disPath, filename);
        // 保存到相应的路径
        file.transferTo(fileObj);
        String webPath = fileConfig.getWebpath().replaceFirst("\\*+", filename);
        return new Result(1, "文件上传成功", webPath);
    }

    @RequestMapping("delImg")
    public Result delImg(String webPath){
        UploadPropertise.FileConfig fileConfig = uploadPropertise.getConfig().get("image");
        String filename = webPath.substring(webPath.lastIndexOf("/")+1);
        String diskpath = fileConfig.getDiskpath();
        File fileObj = new File(diskpath, filename);
        if (fileObj.delete()) {
            return new Result(1,"删除成功", null);
        }else{
            return new Result(0,"删除失败", null);
        }
    }

    @RequestMapping("uploads")
    public Result uploads(@RequestParam("file") MultipartFile[] file,
                          String type) throws IOException {
        UploadPropertise.FileConfig fileConfig = uploadPropertise.getConfig().get(type);
        List<String> filenames = new ArrayList<>();
        for (MultipartFile multipartFile : file) {
            // 修改用户上传文件的文件名, 防止文件同名而覆盖
            String filename = multipartFile.getOriginalFilename();
            String prefix = UUID.randomUUID().toString().replaceAll("-", "");
            String suffix = filename.replaceFirst("(.+)(\\.\\w+)", "$2");
            filename = prefix + suffix;
            filenames.add(filename);
        }
        //文件夹不存在就创建
        String diskpath = fileConfig.getDiskpath();
        File disPath = new File(diskpath);
        if(!diskpath.endsWith("/")){
            diskpath = diskpath +  "/";
        }
        if (!disPath.exists()) {
            disPath.mkdirs();
        }

        List<String> webPaths = new ArrayList<>();
        for (int i = 0; i < file.length; i++) {
            // 保存本地
            File fileObj = new File(disPath, filenames.get(i));
            file[i].transferTo(fileObj);
            // 网络路径
            String webPath = fileConfig.getWebpath().replaceFirst("\\*+", filenames.get(i));
            webPaths.add(webPath);
        }
        return new Result(1, "文件上传成功", webPaths);
    }


}
